<?php
/**
 * Widget Ajax Management
 *
 * @package OWL
 * @subpackage Administration
 * @since 1.0
 * @author trung
 */
require_once "../load.php";
error_reporting(0);
if (get_magic_quotes_gpc()) {
    $_POST = Owl_Format::stripslashes_deep($_POST);
}
switch ( $action = $_POST['action'] ) :
    case 'widgets-order' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        unset( $_POST['savewidgets'], $_POST['action'] );
        if ( is_array($_POST['sidebars']) ) {
            $sidebars = array();
            foreach ( $_POST['sidebars'] as $key => $val ) {
                $sb = array();
                if ( !empty($val) ) {
                    $val = explode(',', $val);
                    foreach ( $val as $k => $v ) {
                        if ( strpos($v, 'widget-') === false )
                            continue;
                        $sb[$k] = substr($v, strpos($v, '-') + 1);
                    }
                }
                $sidebars[$key] = $sb;
            }            
            die($owl_sidebar_control->save_sidebars_widgets($sidebars));
        }
        die('-1');
        break;
    case 'save-widget' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        unset( $_POST['savewidgets'], $_POST['action'] );
        $id_base = $_POST['id_base'];
        $number = $_POST['number'];
        //$settings = isset($_POST['widget-' . $id_base]) && is_array($_POST['widget-' . $id_base]) ? $_POST['widget-' . $id_base] : false;
        $error = '<p>' . 'Có lỗi xảy ra hãy load lại trang' . '</p>';
        $registerd_widgets = $owl_widget_control->get_registered_widgets();
        // delete
        if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
            if ( !isset($registerd_widgets[$id_base]) )
                die($error);
        }
        $registerd_widgets[$id_base]->build_update($number);
        //echo $registerd_widgets[$id_base]->build_form($number);
        echo 1;
        die();
        break;
    case 'show_form' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        unset($_POST['action']);
        $widget_id = $_POST['widget_id'];
        echo $owl_widget_control->get_widget_control($widget_id);
        die();
        break;
    case 'show_display' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        unset($_POST['action']);
        $widget_id = $_POST['widget_id'];
        $sidebar_id = $_POST['sidebar_id'];
        echo $owl_widget_control->rebuild_widget($widget_id, $sidebar_id);
        die();
        break;
    case 'next-number' :
        $user = Owl_User::get_instance();
        if (!$user->is_logged_in()) {
            die();
        }
        unset($_POST['action']);
        $id_base = $_POST['id_base'];
        echo $owl_widget_control->get_next_number($id_base);
        die();
        break;
endswitch;
?>
